﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using ParieurPro.DAL;
using MyBet.Class;
using System.Collections.Generic;

namespace MyBet
{
	public partial class GeneralAdministration : UserControl
	{
        protected class SeasonAdmin
        {
            public int id;
            public String name;

            public override string ToString()
            {
                return name;
            }
        }

        protected class EventAdmin
        {
            public EventServiceReference.EventObject evt;

            public override string ToString()
            {
                return EventHelper.GetEventName(evt);
            }

            public EventAdmin(EventServiceReference.EventObject item)
            {
                evt = item;
            }

        }


		public GeneralAdministration()
		{
			// Required to initialize variables
			InitializeComponent();
		}

        public void LoadList()
        {
            
        }

        /// <summary>
        /// Chargement du control
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            SeasonList.ClearValue(ComboBox.ItemsSourceProperty);
            SeasonAdmin s1 = new SeasonAdmin();
            s1.id = 1;
            s1.name = "Saison 2011-12";
            SeasonList.Items.Add(s1);
            SeasonList.SelectedIndex = 0;

            ChampionshipList.ClearValue(ComboBox.ItemsSourceProperty);
            ChampionshipList.ItemsSource=App.ChampionshipsCache.Values;
            ChampionshipList.DisplayMemberPath = "Name";
            ChampionshipList.SelectedIndex=0;
        }

        /// <summary>
        /// Changement de championnat, mise à jour de la grille des événements
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ChampionshipList_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if ((ChampionshipList.SelectedItem != null) && (SeasonList.SelectedItem != null))
            {
                EventServiceReference.GetEventScoreClient eventSvc = new EventServiceReference.GetEventScoreClient();
                eventSvc.GetGlobalEventListCompleted += new EventHandler<EventServiceReference.GetGlobalEventListCompletedEventArgs>(eventSvc_GetGlobalEventListCompleted);
                eventSvc.GetGlobalEventListAsync((ChampionshipList.SelectedItem as ChampionshipObject).ChampionshipId, (SeasonList.SelectedItem as SeasonAdmin).id);
            }
        }

        /// <summary>
        /// Mise à jour des données de la grille
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void eventSvc_GetGlobalEventListCompleted(object sender, EventServiceReference.GetGlobalEventListCompletedEventArgs e)
        {
            List<EventAdmin> eventListContent = new List<EventAdmin>();
            foreach (EventServiceReference.EventObject item in e.Result)
            {
                eventListContent.Add(new EventAdmin(item));
            }
            EventList.ItemsSource = eventListContent;
            //EventList.DisplayMemberPath = "DisplayName";
            EventList.SelectedIndex = 0;
        }

        /// <summary>
        /// Nouvel événement sélectionné
        /// => Modification des date
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void EventList_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            EventAdmin evt = EventList.SelectedItem as EventAdmin;
            if (evt != null)
            {
                DateTime openDate = UserTimezone.FromUTC(evt.evt._openDate);
                DateTime closeDate = UserTimezone.FromUTC(evt.evt._closeDate);
                EventOpenCalendar.SelectedDate = openDate;
                EventOpenCalendar.DisplayDate = openDate;
                EventCloseCalendar.SelectedDate = closeDate;
                EventCloseCalendar.DisplayDate = closeDate;
                EventOpenHour.Text = String.Format("{0}:{1}", openDate.Hour.ToString().PadLeft(2, '0'), openDate.Minute.ToString().PadLeft(2, '0'));
                EventCloseHour.Text = String.Format("{0}:{1}", closeDate.Hour.ToString().PadLeft(2, '0'), closeDate.Minute.ToString().PadLeft(2, '0'));
            }
        }

        /// <summary>
        /// Mise à jour des dates de l'événement
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void UpdateButton_Click(object sender, RoutedEventArgs e)
        {
            String[] openDateTime=EventOpenHour.Text.Split(':');
            String[] closeDateTime=EventCloseHour.Text.Split(':');
            EventAdmin currentEvent = EventList.SelectedItem as EventAdmin;

            DateTime utcOpenDate = UserTimezone.ToUTC(new DateTime(EventOpenCalendar.SelectedDate.Value.Year,
                                                EventOpenCalendar.SelectedDate.Value.Month,
                                                EventOpenCalendar.SelectedDate.Value.Day,
                                                Convert.ToInt32(openDateTime[0].Trim()),
                                                Convert.ToInt32(openDateTime[1].Trim()),0));

            DateTime utcCloseDate = UserTimezone.ToUTC(new DateTime(EventCloseCalendar.SelectedDate.Value.Year,
                                                EventCloseCalendar.SelectedDate.Value.Month,
                                                EventCloseCalendar.SelectedDate.Value.Day,
                                                Convert.ToInt32(closeDateTime[0].Trim()),
                                                Convert.ToInt32(closeDateTime[1].Trim()),0));

            currentEvent.evt._openDate = utcOpenDate;
            currentEvent.evt._closeDate = utcCloseDate;

            EventServiceReference.GetEventScoreClient eventSvc = new EventServiceReference.GetEventScoreClient();
            eventSvc.SetEventDatesCompleted += new EventHandler<EventServiceReference.SetEventDatesCompletedEventArgs>(eventSvc_SetEventDatesCompleted);
            eventSvc.SetEventDatesAsync((EventList.SelectedItem as EventAdmin).evt._eventId, utcOpenDate, utcCloseDate);
        }

        /// <summary>
        /// Retour de la mise à jour des dates de l'événement
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void eventSvc_SetEventDatesCompleted(object sender, EventServiceReference.SetEventDatesCompletedEventArgs e)
        {
            if (e.Result)
            {
                MessageBox.Show("Event dates has been applied");
            }
            else
            {
                MessageBox.Show("Event update failed !!");
            }
        }

	}
}